package com.calmcar.adas.apiserver.model;

import android.graphics.ImageFormat;

import com.calmcar.adas.apiserver.out.CvCameraViewFrame;

import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;

/**
 * 相机帧数据
 */

public  class JavaCameraFrame implements CvCameraViewFrame {
    @Override
    public Mat gray() {
        return mYuvFrameData.submat(0, mHeight, 0, mWidth);
    }

    @Override
    public Mat rgba() {
        Imgproc.cvtColor(mYuvFrameData, mRgba, getConvertCode(mType), 4);
        return mRgba;
    }

    public JavaCameraFrame(Mat Yuv420sp, int width, int height) {
        super();
        mWidth = width;
        mHeight = height;
        mYuvFrameData = Yuv420sp;
        mRgba = new Mat();
    }

    public JavaCameraFrame(Mat Yuv420sp, int width, int height,int type) {
        super();
        mWidth = width;
        mHeight = height;
        mYuvFrameData = Yuv420sp;
        mRgba = new Mat();
        mType=type;
    }


    private int getConvertCode(int type ){
        int code=Imgproc.COLOR_YUV2RGBA_NV21;
        switch(type){
            case ImageFormat.NV21:
                code=Imgproc.COLOR_YUV2RGBA_NV21;
                break;
            case ImageFormat.YV12:
                code=Imgproc.COLOR_YUV2RGBA_YV12;
                break;
            case -1:
                code=Imgproc.COLOR_YUV2RGBA_NV21;
                break;
        }
        return code;
    }


    public void release() {
        mRgba.release();
    }

    private Mat mYuvFrameData;
    private Mat mRgba;
    private int mWidth;
    private int mHeight;
    private int mType=-1;
};